//
//  RGShareProfileView.m
//  RedGrass
//
//  Created by wuyine on 2018/12/14.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGShareProfileView.h"
#import <UIImageView+WebCache.h>
#import "YQQRCode.h"

@interface RGShareProfileView()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic,strong) UIImageView *qrCodeImage;
@property (nonatomic,strong) UILabel *nickName;
@property (nonatomic,strong) UIButton *closeBtn;
@property (nonatomic, strong) UIImageView *logo;
@property (nonatomic, strong) UIImageView *bgImg;

@end

@implementation RGShareProfileView
- (instancetype)init {
    if (self = [super init]) {
        
        
        self.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.5);
        
        RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
        
        UIImageView *bg = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_白框")];
        bg.userInteractionEnabled = YES;
        _bgImg = bg;
        [self addSubview:bg];
        [bg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(kScreenWidth-20*2, (kScreenWidth-20*2)*400/315));
        }];
        
        
        [bg addSubview:self.qrCodeImage];
        NSDictionary *param = @{@"userId":@([RGUserInfoModel getUserInfo].ID)};
        NSError *parseError = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:&parseError];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        UIImage *qrCodeImg = [YQQRCode enCodeWithString:jsonString withImageSize:KScreenWidth];
        _qrCodeImage.image = qrCodeImg;
        [_qrCodeImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(180, 180));
            if(KIsiPhoneX){
                make.top.equalTo(self).offset(210);
            }else{
                make.top.equalTo(self).offset(170);
            }
          
        }];
        
        UIImageView *logo = [[UIImageView alloc] init];
        [logo sd_setImageWithURL:[NSURL URLWithString:user.image] placeholderImage:IMAGE_NAMED(@"个人主页_头像")];
        ViewBorderRadius(logo, 6, 2, [UIColor whiteColor]);
        _logo = logo;
        [bg addSubview:logo];
        [logo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.qrCodeImage.mas_centerX).mas_offset(0);
            make.centerY.mas_equalTo(self.qrCodeImage.mas_centerY).mas_offset(0);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
                                  
        [bg addSubview:self.nickName];
        [_nickName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.equalTo(self.qrCodeImage.mas_bottom).offset(37);
        }];
        
        UILabel *tip = [[UILabel alloc] init];
        tip.textColor = UICOLOR_WITH_RGBINT(0x999999);
        tip.text = @"使用红草视界扫码，加我好友";
        tip.font = FONT(customFontName, 12);
        [bg addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.equalTo(self.nickName.mas_bottom).offset(5);
        }];
        
        [self addSubview:self.contentView];
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(kScreenWidth-20*2, (kScreenWidth-20*2)*400/315));
        }];
        
        [_contentView addSubview:self.closeBtn];
        [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(50, 50));
            make.top.trailing.mas_equalTo(0);
        }];
        
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareBtn setTitle:@"分享到微信" forState:UIControlStateNormal];
        [shareBtn setTitleColor:UICOLOR_WITH_RGBINT(0x000000) forState:UIControlStateNormal];
        shareBtn.titleLabel.font = FONT(customFontName, 16);
        [shareBtn setImage:IMAGE_NAMED(@"分享_分享") forState:UIControlStateNormal];
        [shareBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        // button标题的偏移量
        shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0,-shareBtn.currentImage.size.width+2,0,shareBtn.currentImage.size.width+2);
        // button图片的偏移量
        shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0,85,0,-85);
        
        [_contentView addSubview:shareBtn];
        [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.mas_equalTo(0);
            make.centerX.mas_equalTo(0);
            make.height.mas_equalTo(55);
            make.bottom.mas_equalTo(0);
        }];
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = UICOLOR_WITH_RGBINT(0xcccccc);
        [_contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(20);
            make.trailing.mas_equalTo(-20);
            make.height.mas_equalTo(0.5);
            make.bottom.equalTo(shareBtn.mas_top).offset(0);
        }];
    }
    return self;
}

- (void)showInView:(UIView *)view {
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

- (void)share:(UIGestureRecognizer *)recong {
    [self removeFromSuperview];
    if(self.shareUserWithShareImgBlock){
        UIImage *shareImg = [self makeImageWithView:_bgImg withSize:CGSizeMake(kScreenWidth-20*2, (kScreenWidth-20*2)*400/315 - 40)];
        self.shareUserWithShareImgBlock(shareImg);
    }
}

-(void)setNick:(NSString *)nick{
    if(nick == nil){
        return;
    }
    _nick = nick;
    self.nickName.text = SafeStr(nick);
}

- (void)closeShare {
    [self removeFromSuperview];
}

#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}


#pragma mark - getter

-(UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}

- (UIImageView *)qrCodeImage {
    if (!_qrCodeImage) {
        _qrCodeImage = [[UIImageView alloc] init];
        _qrCodeImage.image = IMAGE_NAMED(@"二维码_头像");
    }
    return _qrCodeImage;
}

- (UILabel *)nickName {
    if (!_nickName) {
        _nickName = [[UILabel alloc] init];
        _nickName.text = @"昵称";
        _nickName.textColor = UICOLOR_WITH_RGBINT(0x000000);
        _nickName.font = FONT(customFontName, 16);
    }
    return _nickName;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:IMAGE_NAMED(@"分享_关闭") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeShare) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
@end
